package com.jzo2o.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.market.enums.CouponStatusEnum;
import com.jzo2o.market.mapper.CouponMapper;
import com.jzo2o.market.model.domain.Coupon;
import com.jzo2o.market.model.dto.request.CouponOperationPageQueryReqDTO;
import com.jzo2o.market.model.dto.response.CouponInfoResDTO;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.market.service.ICouponUseBackService;
import com.jzo2o.market.service.ICouponWriteOffService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.jzo2o.market.enums.CouponStatusEnum.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-16
 */
@Service
@Slf4j
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {

    @Resource(name = "seizeCouponScript")
    private DefaultRedisScript<String> seizeCouponScript;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private IActivityService activityService;

    @Resource
    private ICouponUseBackService couponUseBackService;

    @Resource
    private ICouponWriteOffService couponWriteOffService;


    //根据活动id查询优惠券领取记录
    @Override
    public PageResult<CouponInfoResDTO> queryPage(CouponOperationPageQueryReqDTO couponOperationPageQueryReqDTO) {
        //根据活动id查询
        Page<Coupon> couponQueryPage = PageUtils.parsePageQuery(couponOperationPageQueryReqDTO, Coupon.class);
        LambdaQueryWrapper<Coupon> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Coupon::getActivityId, couponOperationPageQueryReqDTO.getActivityId());

        Page<Coupon> couponPage = baseMapper.selectPage(couponQueryPage, queryWrapper);

        PageResult<CouponInfoResDTO> resDTOPageResult = PageUtils.toPage(couponPage, CouponInfoResDTO.class);

        return resDTOPageResult;
    }

    //优惠券作废
    @Override
    public void revoke(Long id) {

        boolean result = lambdaUpdate()
                .eq(Coupon::getActivityId, id)
                .eq(Coupon::getStatus, NO_USE.getStatus())
                .set(Coupon::getStatus, CouponStatusEnum.VOIDED.getStatus())
                .update();

        if(!result){
            throw new DBException("优惠券作废失败");
        }
    }

    //我的优惠券列表
    @Override
    public List<CouponInfoResDTO> myCouponList(Integer status, Long lastId) {

        ArrayList<Integer> couponStatusList = new ArrayList<>();
        couponStatusList.add(NO_USE.getStatus());
        couponStatusList.add(USED.getStatus());
        couponStatusList.add(INVALID.getStatus());

        if(!couponStatusList.contains(status)){
            throw new BadRequestException("当前查询的优惠券状态错误");
        }

        Long userId = UserContext.currentUserId();

        List<Coupon> list = this.lambdaQuery()
                .eq(Coupon::getUserId, userId)
                .eq(Coupon::getStatus, status)
                .lt(ObjectUtils.isNotEmpty(lastId), Coupon::getId, lastId)
                .list();

        List<CouponInfoResDTO> resultList = BeanUtils.copyToList(list, CouponInfoResDTO.class);

        return resultList;
    }
}
